/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pushy.client;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;

/**
 *
 * @author dörte
 * Hauptklasse des Clients
 * JFrame, welcher je nach Zustand neue JPanels als Inhalt lädt
 * Erstellt zudem einen Client, welcher wiederum auf dem ZA-Klassen-Client basiert
 */
public class GUI extends javax.swing.JFrame {
    
    private Client client;  //Client
    private JPanel panel;   //Das aktuelle panel
    private Levelansicht levelansicht;  //Die Levelansicht (wichtig, weil diese das aktuelle Level beinhaltet)

    /**
     * Creates new form GUI
     */
    public GUI() {
        initComponents();
        
        client = new Client("localhost", 1234, this); //Es wird davon ausgegangen, dass der Server bereits gestartet ist!!
        Anmeldebildschirm();
        
        levelansicht = new Levelansicht(client);
        
        addWindowListener(new WindowAdapter()
        {
            /**
             * Methode meldet den Client ab und beendet das Programm, wenn das Fenster geschlossen wird
             * @param e 
             */
            public void windowClosing(WindowEvent e)
            {
                client.close();
                System.exit(0);
            }
        });
    }
    
    public Levelansicht getLevelansicht() { return levelansicht; }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${defaultCloseOperation}"), this, org.jdesktop.beansbinding.BeanProperty.create("defaultCloseOperation"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Räumt die Oberfläche auf
     * Notwendig für die Levelansicht
     */
    private void clear() {
        this.setSize(600, 500);
        
        getContentPane().removeAll();
        getContentPane().repaint();
        getContentPane().revalidate();
    }
    
    public JPanel getPanel() {
        return panel;
    }
    
    /**
     * Das Hauptmenü bzw der Startbildschirm wird geladen
     */
    public void MainMenu() {
        clear();
        panel = new MainMenu(client);
        add(panel);
        this.setLocationRelativeTo(null);
        setSize(995, 550);
        this.setResizable(false);
    }
    
    /**
     * Der Anmeldebildschirm wird geladen
     */
    public void Anmeldebildschirm() {
        clear();
        this.setSize(570, 150);
        this.setLocationRelativeTo(null);
        panel = new Anmeldebildschirm(client);
        add(panel);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
    }
    
    /**
     * Der Matchgenerator wird geladen
     * Beinhaltet die Onlineliste so wie ein Feld mit dem man Spieler herausfordern kann
     */
    public void Challenge() {
        clear();
        setSize(500, 400);
        panel = new Challenge(client);
        add(panel);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
    }
    
    /**
     * Das Level wird angezeigt so wie das Spielfeld
     */
    public void Levelansicht() {
        clear();
        setSize(1512, 970);
        panel = levelansicht;
        add(panel);
        addKeyListener((Levelansicht) panel);
        panel.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setResizable(true);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
